//
//  DHHomeCategoriesCell.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/4/22.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHHomeCategoriesCell.h"
#import "DHHomeCategories_CollectionCell.h"
#import "DHProductListController.h"

@interface DHHomeCategoriesCell()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (strong, nonatomic) NSArray *itemArr; //item数组

@end

@implementation DHHomeCategoriesCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"DHHomeCategories_CollectionCell" bundle:nil] forCellWithReuseIdentifier:kDHHomeCategories_CollectionCell];
    
//    NSString *categorySex = [GlobalManager shareGlobalManager].user.sex? :[[NSUserDefaults standardUserDefaults] objectForKey:USER_SELECT_SEX];
    if ([Login isLogin]) {
        if ([[GlobalManager shareGlobalManager].user.sex isEqualToString:@"男"]) {
            [self.manImgView setImage:[UIImage imageNamed:@"home_category_selected"]];
            [self.womanImgView setImage:[UIImage imageNamed:@"home_category_unselected"]];
        }else{
            [self.manImgView setImage:[UIImage imageNamed:@"home_category_unselected"]];
            [self.womanImgView setImage:[UIImage imageNamed:@"home_category_selected"]];
        }
    }else{
        NSString *sex = [[NSUserDefaults standardUserDefaults] objectForKey:USER_SELECT_SEX];
        if ([sex isEqualToString:@"男"]) {
            [self.manImgView setImage:[UIImage imageNamed:@"home_category_selected"]];
            [self.womanImgView setImage:[UIImage imageNamed:@"home_category_unselected"]];
        }else{
            [self.manImgView setImage:[UIImage imageNamed:@"home_category_unselected"]];
            [self.womanImgView setImage:[UIImage imageNamed:@"home_category_selected"]];
        }
    }
    
    WeakSelf(weakSelf)
    [self.manButton bk_addEventHandler:^(id sender) {
        [weakSelf.manImgView setImage:[UIImage imageNamed:@"home_category_selected"]];
        [weakSelf.womanImgView setImage:[UIImage imageNamed:@"home_category_unselected"]];
        [weakSelf routerEventWithName:@"man_cagetory" userInfo:nil];
    } forControlEvents:UIControlEventTouchUpInside];
    [self.womanButton bk_addEventHandler:^(id sender) {
        [weakSelf.manImgView setImage:[UIImage imageNamed:@"home_category_unselected"]];
        [weakSelf.womanImgView setImage:[UIImage imageNamed:@"home_category_selected"]];
        [weakSelf routerEventWithName:@"woman_cagetory" userInfo:nil];
    } forControlEvents:UIControlEventTouchUpInside];
}

- (NSArray *)itemArr{
    if (!_itemArr) {
        _itemArr = [NSArray array];
    }
    return _itemArr;
}

- (void)updateCellWithArray:(NSArray *)arr{
    self.itemArr = arr;
    [self.collectionView reloadData];
}

#pragma mark - UICollectionViewDataSource and UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.itemArr.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    DHHomeCategories_CollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kDHHomeCategories_CollectionCell forIndexPath:indexPath];
    NSDictionary *dic = self.itemArr[indexPath.row];
    [cell.imagView dh_setImageURLString:[dic objectForKey:@"icon"]];
    cell.titleLabel.text = [dic objectForKey:@"title"];
    return cell;
}

#pragma mark ---- UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return (CGSize){(kScreenWidth - 16 - 8)/3,100};
}

// 两行之间的最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

// 两个cell之间的最小间距，是由API自动计算的，只有当间距小于该值时，cell会进行换行
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 4;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *model = [self.itemArr objectAtIndex:indexPath.row];
    DHProductListController *vc = [[DHProductListController alloc] init];
    vc._id = [model objectForKey:@"_id"];
    vc.paramsKey = @"category_id";
    vc.navTitle = [model objectForKey:@"title"];
    vc.hidesBottomBarWhenPushed = YES;
    [self.viewController.navigationController pushViewController:vc animated:YES];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
